// --------------------------------------------------------------------
//   name 	: BBCoded Reports
//   desc 	: Shows a button which lets you convert your reports to BBCode to make it look better on the forum
//   version	: Beta v0.1
//   dev 	: JelianSD
// --------------------------------------------------------------------
// DEPENDENCIES
// 	None
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1	Initial Release
// 0.2 Fixed a bug with the scouted resource
// --------------------------------------------------------------------

plugins.BBCodesReport = {

//BB Code generator by JelianSD, pre-beta v1
enhance_game_report : function() {
  
setFunc("get_report", function(){
  var ReportAtkVil = (document.getElementById("AtkVillage").checked)?true:false;
  var ReportAtkTro = (document.getElementById("AtkTroops").checked)?true:false;
  var ReportDefVil = (document.getElementById("DefVillage").checked)?true:false;
  var ReportDefTro = (document.getElementById("DefTroops").checked)?true:false;
  var ReportHaul = (document.getElementById("RepHaul").checked)?true:false;
  var ReportScoHaul = (document.getElementById("ScoHaul").checked)?true:false;
  var ReportScoBuild = (document.getElementById("ScoBuild").checked)?true:false;
  var ReportExtraMSG = (document.getElementById("ExtraMSG").checked)?true:false;
  window.setTimeout(function() {
    setValuePlugin('BBReportAtkVil',ReportAtkVil);
	setValuePlugin('BBReportAtkTro',ReportAtkTro);
	setValuePlugin('BBReportDefVil',ReportDefVil);
	setValuePlugin('BBReportDefTro',ReportDefTro);
	setValuePlugin('BBRepHaul',ReportHaul);
	setValuePlugin('BBScoHaul',ReportScoHaul);
	setValuePlugin('BBScoBuild',ReportScoBuild);
	setValuePlugin('BBExtraMSG',ReportExtraMSG);
  }, 0); 
  
  var SentImage = $("table.vis:eq(1) tr:first th:last img")[0];
  SentImage = (SentImage)?SentImage.src:false;
  var SentSubject = $("table.vis:eq(1) tr:first th:last span:first span:first")[0];
  SentSubject = (SentSubject)?SentSubject.innerHTML:false;
  var SentTime = $("table.vis:eq(1) tr:eq(1) td:last")[0];
  SentTime = (SentTime)?SentTime.innerHTML:false;
  
  var Luck = $("table:has(img[src*='klee']) b")[0];
  Luck = (Luck)?Luck.innerHTML:false;
  
  var Morale = $("h4")[1];
  Morale = (Morale)?Morale.innerHTML.split(" ")[1]:false;
  
  var ReportMessage = $("h3")[0];
  ReportMessage = (ReportMessage)?ReportMessage.innerHTML:false;
  
  var attacker = $("table[width='100%']:has(table.vis img[src*='graphic/unit/unit_spear.png']):eq(0)")[0];
  if(attacker) {
	  var AttackPlayerName = $('tr:first > th:eq(1) a', attacker)[0];
	  AttackPlayerName = (AttackPlayerName)?AttackPlayerName.innerHTML:false;
	  var AttackPlayerVillage = $('tr:eq(1) > td:eq(1) a', attacker)[0];
	  AttackPlayerVillage = (AttackPlayerVillage)?AttackPlayerVillage.innerHTML.match(/\((-?\d+\|-?\d+)\)/)[1]:false;
	  var AttackTroops = $('tr:eq(2) table.vis', attacker)[0];
	  var AttackTroopsTotal = "";
	  var AttackTroopsLoss = "";
	  if(AttackTroops) {
		  for(var i=0;i<12;i++) {
			var AttackTroopsCurrent = $('tr:eq(1) td:eq('+(1+i)+')', AttackTroops)[0];
			if(AttackTroopsCurrent) {
				AttackTroopsCurrent = parseInt( AttackTroopsCurrent.innerHTML );
				if(AttackTroopsCurrent>0) {
					AttackTroopsTotal += AttackTroopsCurrent+"[img]graphic/"+UnitList[i]+"[/img] ";
				}
			}
			var AttackTroopsCurrent = $('tr:eq(2) td:eq('+(1+i)+')', AttackTroops)[0];
			if(AttackTroopsCurrent) {
				AttackTroopsCurrent = parseInt( AttackTroopsCurrent.innerHTML );
				if(AttackTroopsCurrent>0) {
					AttackTroopsLoss += AttackTroopsCurrent+"[img]graphic/"+UnitList[i]+"[/img] ";
				}
			}
		  }
	  } else {
		var AttackTroopsTotal = false;
		var AttackTroopsLoss = false;
	  }
  } else {
	  var AttackPlayerName = false;
	  var AttackPlayerVillage = false;
	  var AttackTroopsTotal = false;
	  var AttackTroopsLoss = false;
  }
  
  var defender = $("table[width='100%']:has(table.vis img[src*='graphic/unit/unit_spear.png']):eq(1)")[0];
  if(defender) {
	  var DefendPlayerName = $('tr:first > th:eq(1) a', defender)[0];
	  DefendPlayerName = (DefendPlayerName)?DefendPlayerName.innerHTML:false;
	  var DefendPlayerVillage = $('tr:eq(1) > td:eq(1) a', defender)[0];
	  DefendPlayerVillage = (DefendPlayerVillage)?DefendPlayerVillage.innerHTML.match(/\((-?\d+\|-?\d+)\)/)[1]:false;
	  var DefendTroops = $('tr:eq(2) table.vis', defender)[0];
	  var DefendTroopsTotal = "";
	  var DefendTroopsLoss = "";
	  if(DefendTroops) {
		  for(var i=0;i<12;i++) {
			var DefendTroopsCurrent = $('tr:eq(1) td:eq('+(1+i)+')', DefendTroops)[0];
			if(DefendTroopsCurrent) {
				DefendTroopsCurrent = parseInt( DefendTroopsCurrent.innerHTML );
				if(DefendTroopsCurrent>0) {
					DefendTroopsTotal += DefendTroopsCurrent+"[img]graphic/"+UnitList[i]+"[/img] ";
				}
			}
			var DefendTroopsCurrent = $('tr:eq(2) td:eq('+(1+i)+')', DefendTroops)[0];
			if(DefendTroopsCurrent) {
				DefendTroopsCurrent = parseInt( DefendTroopsCurrent.innerHTML );
				if(DefendTroopsCurrent>0) {
					DefendTroopsLoss += DefendTroopsCurrent+"[img]graphic/"+UnitList[i]+"[/img] ";
				}
			}
		  }
	  } else {
		var DefendTroopsTotal = false;
		var DefendTroopsLoss = false;
	  }
  } else {
	  var DefendPlayerName = false;
	  var DefendPlayerVillage = false;
	  var DefendTroopsTotal = false;
	  var DefendTroopsLoss = false;
  }
  
  var ScoutedResource = $("td:not(td[width='220']):has(> img[src$='graphic/holz.png'])")[0];
  if(ScoutedResource) {
    if(!ScoutedResource.innerHTML.match(/<u.*/g)) {
	 var ScoResources = ScoutedResource.innerHTML.replace(/<span.*?<\/span>/g,"").replace(/<img.*?>/g,"").split(" ");
	 if(ScoResources.length>2) {
		var ScoutedResource = "";
		ScoutedResource = ScoResources[0]+" [img]graphic/holz.png[/img] "+ScoResources[1]+" [img]graphic/lehm.png[/img] "+ScoResources[2]+" [img]graphic/eisen.png[/img]";
	 } else {
		var ScoutedResource = false;
	 }
	} else {
	    var ScoutedResource = false;
	}
  } else {
	 var ScoutedResource = false;
  }
  
  var Haul = $("td[width='220']:has(> img[src$='graphic/holz.png'])")[0];
  if(Haul) {
	 var HaulResources = Haul.innerHTML.replace(/<span.*?<\/span>/g,"").replace(/<img.*?>/g,"").split(" ");
	 if(HaulResources.length>2) {
		var Haul = "";
		Haul = HaulResources[0]+" [img]graphic/holz.png[/img] "+HaulResources[1]+" [img]graphic/lehm.png[/img] "+HaulResources[2]+" [img]graphic/eisen.png[/img]";
	 } else {
		var Haul = false;
	 }
  } else {
	 var Haul = false;
  }
  
  var Buildings = $('tr:has(> td > img[src$="graphic/holz.png"]) + tr:first > td')[0];
  if(Buildings) {
	 if(!Buildings.innerHTML.match(/<a.*>.*<\/a>/g) && Buildings.innerHTML.match(/\n/g)) {
	     if(Buildings.innerHTML.match(/\n/g).length>5) {
			Buildings = Buildings.innerHTML.replace(/	/g,"").replace(/<br.*>/g,"").replace(/<b>/g,"[b]").replace(/<\/b>/g,"[/b]");
		 } else {
			Buildings = false;
		 }
	 } else {
		 Buildings = false;
	 }
  } else {
	 Buildings = false;
  }
  
  var ExtraText = $("table[width='100%']:eq(2) td[colspan='2']");
  var ExtraTextBBCode = "";
  for(var i=0;i<ExtraText.length;i++) {
     if(ExtraText[i]) {
		ExtraTextBBCode += ExtraText[i].innerHTML.replace(/	/g,"").replace(/<br.*>/g,"").replace(/\n/g,"").replace(/<b>/g,"[b]").replace(/<\/b>/g,"[/b]")+"\n\n";
	 }
  }  
  
 /* alert("SentImage:"+SentImage
	+"\nSentSubject:"+SentSubject
	+"\nSentTime:"+SentTime
	
	+"\nLuck:"+Luck
	+"\nMorale:"+Morale
	+"\nMSG:"+ReportMessage
	
	+"\nAttackerName:"+AttackPlayerName
	+"\nAttackPlayerVillage:"+AttackPlayerVillage
	+"\nAttackTroopsTotal:"+AttackTroopsTotal
	+"\nAttackTroopsLoss:"+AttackTroopsLoss
	
	+"\nDefendName:"+DefendPlayerName
	+"\nDefendPlayerVillage:"+DefendPlayerVillage
	+"\nDefendTroopsTotal:"+DefendTroopsTotal
	+"\nDefendTroopsLoss:"+DefendTroopsLoss
	
	+"\nHaul:"+Haul
	+"\nBuildings:"+Buildings
	
	+"\nExtraBBCode:"+ExtraTextBBCode); */
  var reportBB = "";
  reportBB += "[b][i]Subject[/i][/b]: [img]"+SentImage+"[/img] "+SentSubject+"";
  reportBB += "\n[b][i]Sent[/i][/b]: "+SentTime+"";
  /*Main report message*/
  if(ReportMessage) reportBB += "\n\n"+ReportMessage+"\n";
  /* Luck */
  if(parseInt(Luck)<0) {
	reportBB += "\n[b]Luck:[/b] "+Luck+" [img]graphic/rabe.png[/img]";
  } else {
	reportBB += "\n[b]Luck:[/b] "+Luck+" [img]graphic/klee.png[/img]";
  }
  reportBB += "\n[b]Morale:[/b] "+Morale;
  
  /* Attacking player section */
if(ReportAtkVil==true) {
  if(AttackPlayerName) {
	 reportBB += "\n\n[b]Attacker:[/b] [player]"+AttackPlayerName+"[/player]";
  } else {
     reportBB += "\n\n[b]Attacker:[/b] Unknown";
  }
  reportBB += "\n[b]Village:[/b] [village]"+AttackPlayerVillage+"[/village]";
} else {
	reportBB += "\n\n[b]Attacker:[/b] Hidden";
	reportBB += "\n[b]Village:[/b] Hidden";
}
if(ReportAtkTro==true) {
  reportBB += "\n\n[b][i][u]Quantity:[/u][/i][/b]\n\n";
  if(AttackTroopsTotal!="") {
	 reportBB += AttackTroopsTotal;
  } else { 
	 reportBB += "None";
  }
  reportBB += "\n\n[b][i][u]Losses:[/u][/i][/b]\n\n";
  if(AttackTroopsLoss!="") {
	 reportBB += AttackTroopsLoss; 
  } else {
	 reportBB += "None";
  }
} else {
	reportBB += "\n\n[b][i][u]Quantity:[/u][/i][/b]\n\nHidden";
	reportBB += "\n\n[b][i][u]Losses:[/u][/i][/b]\n\nHidden";
}
  
  /* Deffending player section */
if(ReportDefVil==true) {
  if(DefendPlayerName) {
	 reportBB += "\n\n[b]Defender:[/b] [player]"+DefendPlayerName+"[/player]";
  } else {
     reportBB += "\n\n[b]Defender:[/b] Unknown";
  }
  reportBB += "\n[b]Village:[/b] [village]"+DefendPlayerVillage+"[/village]";
} else {
	reportBB += "\n\n[b]Attacker:[/b] Hidden";
	reportBB += "\n[b]Village:[/b] Hidden";
}
if(ReportDefTro==true) {
  reportBB += "\n\n[b][i][u]Quantity:[/u][/i][/b]\n\n";
  if(DefendTroopsTotal!="") {
	 reportBB += DefendTroopsTotal;
  } else { 
	 reportBB += "None";
  }
  reportBB += "\n\n[b][i][u]Losses:[/u][/i][/b]\n\n";
  if(DefendTroopsLoss!="") {
	 reportBB += DefendTroopsLoss; 
  } else {
	 reportBB += "None";
  }
} else {
	reportBB += "\n\n[b][i][u]Quantity:[/u][/i][/b]\n\nHidden";
	reportBB += "\n\n[b][i][u]Losses:[/u][/i][/b]\n\nHidden";
}
  /*Haul*/
if(ReportHaul==true) {
  if(Haul) {
	 reportBB += "\n\n[u][i]Haul:[/i][/u] "+Haul;
  }
} else {
	//reportBB += "\n\n[u][i]Haul:[/i][/u] Hidden";
}
  /*Scouted Resources*/
if(ReportScoHaul==true) {
  if(ScoutedResource) {
	 reportBB += "\n\n[b][u][i]Scouted Resources:[/i][/u][/b] "+ScoutedResource;
  }
} else {
	//reportBB += "\n\n[b][u][i]Scouted Resources:[/i][/u][/b] Hidden";
}
  /*Buildings*/
if(ReportScoBuild==true) {
  if(Buildings) {
	 reportBB += "\n\n[b][i][u]Buildings:[/u][/i][/b]\n "+Buildings;
  }
} else {
	//reportBB += "\n\n[b][i][u]Buildings:[/u][/i][/b]\n\nHidden";
}
  /*Extra text messages*/
  if(ExtraTextBBCode!="" && ReportExtraMSG==true) {
	 reportBB += "\n\n"+ExtraTextBBCode;
  }
  
  $("#ReportPlaceHolder").html("<textarea style='width:100%;height:400px;'>"+reportBB+"</textarea>");
 
});

if($("table:has(img[src*='klee']) b")[0]) {
  $("td[height='160']").append("<br /><center><a name='BBCodesReport'>&nbsp;</a><div id='BBCodesDiv' style='width:225px;text-align:left;display:"+(getValuePlugin('ShowBBReportsDefault')?"block":"none")+";'><br />"+
				"<input type='checkbox' id='AtkVillage'"+( (getValuePlugin('BBReportAtkVil')!=false)?' checked':' ' )+"><label for='AtkVillage'>Show attacker village</label><br />"+
				"<input type='checkbox' id='AtkTroops'"+( (getValuePlugin('BBReportAtkTro')!=false)?' checked':' ' )+"><label for='AtkTroops'>Show attacker troops</label><br />"+
				"<input type='checkbox' id='DefVillage'"+( (getValuePlugin('BBReportDefVil')!=false)?' checked':' ' )+"><label for='DefVillage'>Show attacker village</label><br />"+
				"<input type='checkbox' id='DefTroops'"+( (getValuePlugin('BBReportDefTro')!=false)?' checked':' ' )+"><label for='DefTroops'>Show defender troops</label><br />"+
				"<input type='checkbox' id='RepHaul'"+( (getValuePlugin('BBRepHaul')!=false)?' checked':' ' )+"><label for='RepHaul'>Show carried haul</label><br />"+
				"<input type='checkbox' id='ScoHaul'"+( (getValuePlugin('BBScoHaul')!=false)?' checked':' ' )+"><label for='ScoHaul'>Show scouted resources</label><br />"+
				"<input type='checkbox' id='ScoBuild'"+( (getValuePlugin('BBScoBuild')!=false)?' checked':' ' )+"><label for='ScoBuild'>Show scouted buildings</label><br />"+
				"<input type='checkbox' id='ExtraMSG'"+( (getValuePlugin('BBExtraMSG')!=false)?' checked':' ' )+"><label for='ExtraMSG'>Show nobeled/ram/catapult info</label><br />"+
				"<center><button onclick='javascript:get_report();'>Create forum BB report!</button></center><br /></div></center><br /><div id='ReportPlaceHolder' style='width:100%;'></div><a href='#BBCodesReport' onclick='document.getElementById(\"BBCodesDiv\").style.display=\"block\";this.style.display=\"none\";' style='display:"+(getValuePlugin('ShowBBReportsDefault')?"none":"block")+";'>Show BB reports Convertor</a>");
}
},
  'info' : {
	  'mandatory' : false,
   'name' : "BBCoded Reports",
   'desc' : "Adds a button to generate BB coded version of the report.",
   'version' : "0.2",
   'dev' : "JelianSD"
  },
  'variables' : {
    'tooltip':["","Show attacker village","Show attacker troops","Show defender village","Show defender troops","Show carried haul","Show scouted resources","Show scouted buildings","Show nobel/ram/catapult info"],
	'desc':["Show/Hide the form for the reports generator by default."],
	ShowBBReportsDefault:false,
	BBReportAtkVil:true,
	BBReportAtkTro:true,
	BBReportDefVil:true,
	BBReportDefTro:true,
	BBRepHaul:true,
	BBScoHaul:true,
	BBScoBuild:true,
	BBExtraMSG:true
  }
 
};